home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 879 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.2 KB  |  60 lines

  1. Path: news.ov.com!news
  2. From: glenn@ov.com (Fletcher.Glenn@ov.com)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Q: Terminating program at EOF
  5. Date: 9 Jan 1996 21:45:29 GMT
  6. Organization: OpenVision
  7. Message-ID: <4cunlp$flp@spanky.pls.ov.com>
  8. References: <4cn66j$5r0@fnpx20.fnal.gov>
  9. Reply-To: glenn@ov.com
  10. NNTP-Posting-Host: foghorn.pls.ov.com
  11.  
  12. In article 5r0@fnpx20.fnal.gov, sfield@fnpx20.fnal.gov (Stephen Field) writes:
  13. >Hi,
  14. >
  15. >I'm writing a program that looks at poorly formatted text files and reformats
  16. >them to a user-specified line length. The input text file has a blank line
  17. >between paragraphs and I would like the newly formatted file to also have blank
  18. >lines between paragraphs.
  19. >
  20. >I've written a program that does the job, but it doesn't stop when it reaches
  21. >the end of the file. The program will write out the reformatted file, but it
  22. >appends a lot of "extra" characters to the end of the file and only stops when
  23. >I break out of it.
  24. >
  25. >I have tried this program on many files and it behaves the same. The program
  26. >is included below and I've got it running on an SGI running IRIX.
  27. >
  28. >Any help appreciated.
  29. >
  30. >Steve
  31. >
  32. >===================================================
  33. >/*    This program reformats a file that is input by the user to not exceed
  34. >    a number of columns that is also input and outputs the reformatted file
  35. >    to a file also given by the user at runtime. Lines are broken at spaces. */
  36. >#include <stdio.h>
  37. >#define MAX_WORD_LENGTH 80    /* max length of word in input */
  38. >
  39. >main()
  40. >{
  41. >  int line_length,        /* max length of line in output */
  42. >      curr_line_length = 0,    /* # of chars printed so far in curr line */
  43. >      word_length;        /* length of current word */
  44. >  char word[ MAX_WORD_LENGTH+1];/* buffer to read word +1 for terminating
  45. >                    '\0' */
  46. >  char c_old=' ',c_new=' ';     /* check for newlines with chars */
  47. >
  48. [snipped irrelevant code]
  49.  
  50. Your problem is that fgetc() returns an int, and c_new is a char.  It is
  51. not possible to test a char for EOF as EOF is bigger than a char.
  52.  
  53.             Fletcher.Glenn@ov.com
  54.  
  55. >/*  while((c_new=fgetc(fptr_read))!=EOF){ */ /* old method for removing EOF bug */
  56. >    while(c_new!=EOF){                       /* new method, still doesn't work */
  57. >      c_new=fgetc(fptr_read);
  58. [snipped more irrelevant code]
  59.  
  60.